package com.ksd.quartz.service;

import com.ksd.common.config.PlayControlConfig;
import com.ksd.common.enums.SourceTypeEnum;
import com.ksd.common.excel.ExcelReader;
import com.ksd.common.excel.MovieExcel;
import com.ksd.common.excel.MusicExcel;
import com.ksd.common.excel.MusicWordTool;
import com.ksd.common.tool.ChinesePinyinConverter;
import com.ksd.common.tool.FileMoveUsingIO;
import com.ksd.common.utils.StringUtils;
import com.ksd.system.domain.*;
import com.ksd.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class MusicExcelService {
    private static final Logger logger = LoggerFactory.getLogger(MusicExcelService.class);
    @Autowired
    private ISysMusicDataService sysMusicDataService;
    @Autowired
    private ISysMusicAlbumService sysMusicAlbumService;
    @Autowired
    private ISysTvDataService sysTvDataService;
    @Autowired
    private ISysBoardService sysBoardService;

//    public void insertReadExcelByRootPathSingle(String fileRootPath,MusicExcel musicExcel,String taskId,Integer sourceType){
//        logger.info("待导入文件的根目录:"+fileRootPath+"单独导入歌曲,数据源:"+sourceType);
//
//        SysTvData sysTvData = new SysTvData();
//        sysTvData.setName(musicExcel.getFileType());
//        sysTvData.setTypeId(Long.valueOf(SysTvType.songDefaultId));
//        List<SysTvData> sysTvDataList = sysTvDataService.selectSysTvDataList(sysTvData);
//        if (StringUtils.isNotNull(sysTvDataList)){
//            sysTvData = sysTvDataList.get(0);
//        }else{
//            sysTvDataService.insertSysTvData(sysTvData);
//        }
//
//        //录入专辑
//        SysMusicAlbum sysMusicAlbum = new SysMusicAlbum();
//        sysMusicAlbum.setTitle(musicExcel.getAlbumTitle());
//        sysMusicAlbum.setFileType(sysTvData.getId());
//        List<SysMusicAlbum> sysMusicAlbumList = sysMusicAlbumService.selectSysMusicAlbumList(sysMusicAlbum);
//        Boolean musicAlbumsFlag = false;
//        if (sysMusicAlbumList.size() > 0) {
//            sysMusicAlbum = sysMusicAlbumList.get(0);
//            musicAlbumsFlag = true;
//        }
//        String logoAddress = musicExcel.getLogoAddress();
//        File logoFile = new File(fileRootPath+File.separator +logoAddress);
//        if (logoFile.exists()) {
//            sysMusicAlbum.setImgAddress(logoFile.getAbsolutePath());
//            if (sourceType.equals(SourceTypeEnum.init.getCode())){
//                String substring = sysMusicAlbum.getImgAddress().substring(PlayControlConfig.getNginxRootPath().length());
//                sysMusicAlbum.setImgUrl(PlayControlConfig.getMappingPath()+substring);
//            }else{
//                String substring = sysMusicAlbum.getImgAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
//                sysMusicAlbum.setImgUrl(PlayControlConfig.getDiskMappingPath()+substring);
//            }
//        }else{
//            return;
//        }
//        String albumTitlekeyWord = ChinesePinyinConverter.convertToPinyin(musicExcel.getAlbumTitle()).replaceAll("\\s+", "");
//        sysMusicAlbum.setKeyWord(albumTitlekeyWord);
//        if (musicAlbumsFlag) {
//            sysMusicAlbumService.updateSysMusicAlbum(sysMusicAlbum);
//        }else{
//            logger.info("新增专辑:"+musicExcel.getAlbumTitle());
//            sysMusicAlbumService.insertSysMusicAlbum(sysMusicAlbum);
//        }
//
//        //开始进行歌曲录入
//        SysMusicData sysMusicData = new SysMusicData();
//        sysMusicData.setTitle(musicExcel.getTitle());
//        sysMusicData.setSinger(musicExcel.getSinger());
//        sysMusicData.setAlbumId(sysMusicAlbum.getId());
//        sysMusicData.setTypeData(sysTvData.getId());
//        List<SysMusicData> sysMusicDataList = sysMusicDataService.selectSysMusicDataList(sysMusicData);
//        Boolean musicDataFlag = false;
//        if (sysMusicDataList.size() > 0) {
//            sysMusicData = sysMusicDataList.get(0);
//            musicDataFlag = true;
//        }
//        String songAddress = musicExcel.getVideoAddress();
//        File songFile = new File(fileRootPath+File.separator +songAddress);
//        if (songFile.exists()) {
//            sysMusicData.setVideoAddress(songFile.getAbsolutePath());
//            if (sourceType.equals(SourceTypeEnum.init.getCode())){
//                String substring = sysMusicData.getVideoAddress().substring(PlayControlConfig.getNginxRootPath().length());
//                sysMusicData.setMusicUrl(PlayControlConfig.getMappingPath()+substring);
//            }else{
//                String substring = sysMusicData.getVideoAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
//                sysMusicData.setMusicUrl(PlayControlConfig.getDiskMappingPath()+substring);
//            }
//
//        }else {
//            return;
//        }
//
//        String lyricAddress = musicExcel.getLyricAddress();
//        File lyricFile = new File(fileRootPath+File.separator +lyricAddress);
//        if (lyricFile.exists()) {
//            sysMusicData.setLyricAddress(lyricFile.getAbsolutePath());
//            if (sourceType.equals(SourceTypeEnum.init.getCode())){
//                String substring = sysMusicData.getLyricAddress().substring(PlayControlConfig.getNginxRootPath().length());
//                sysMusicData.setLyricUrl(PlayControlConfig.getMappingPath()+substring);
//            }else {
//                String substring = sysMusicData.getLyricAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
//                sysMusicData.setLyricUrl(PlayControlConfig.getDiskMappingPath()+substring);
//            }
//        }else {
//            return;
//        }
//        sysMusicData.setAlbumTitle(musicExcel.getAlbumTitle());
//        sysMusicData.setAlbumId(sysMusicAlbum.getId());
//        sysMusicData.setTypeData(sysTvData.getId());
//        String titlekeyWord = ChinesePinyinConverter.convertToPinyin(musicExcel.getTitle()).replaceAll("\\s+", "");
//        sysMusicData.setKeyWord(titlekeyWord);
//        sysMusicData.setContentTitle(musicExcel.getAlbumTitle());
//        sysMusicData.setImgAddress(sysMusicAlbum.getImgAddress());
//        sysMusicData.setTaskId(taskId);
//        sysMusicData.setImgUrl(sysMusicAlbum.getImgUrl());
//        sysMusicData.setSouceType(sourceType);
//        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
//        String s = MusicWordTool.serverReadFile(sysBoard.getUrl()+sysMusicData.getLyricUrl());
//        sysMusicData.setSongWord(s);
//        if (musicDataFlag) {
//            logger.info("更新歌曲:"+sysMusicData.getTitle()+"========="+sysMusicData.getVideoAddress()+"^^^^^^^^^^"+taskId+"数据源类型:"+sourceType);
//            sysMusicDataService.updateSysMusicData(sysMusicData);
//        }else {
//            logger.info("新增歌曲:"+sysMusicData.getTitle()+"^^^^^^^^^^"+taskId+"数据源类型:"+sourceType);
//            sysMusicDataService.insertSysMusicData(sysMusicData);
//        }
//    }
    public void insertReadExcelByRootPath(String fileRootPath,List<MusicExcel> musicExcels,String taskId,Integer sourceType){
        logger.info("待导入文件的根目录:"+fileRootPath+"待导入的歌曲个数:"+musicExcels.size());
        for (MusicExcel musicExcel : musicExcels) {

            SysTvData sysTvData = new SysTvData();
            sysTvData.setName(musicExcel.getFileType());
            sysTvData.setTypeId(Long.valueOf(SysTvType.songDefaultId));
            List<SysTvData> sysTvDataList = sysTvDataService.selectSysTvDataList(sysTvData);
            if (StringUtils.isNotNull(sysTvDataList)){
                sysTvData = sysTvDataList.get(0);
            }else{
                sysTvDataService.insertSysTvData(sysTvData);
            }

            //录入专辑
            SysMusicAlbum sysMusicAlbum = new SysMusicAlbum();
            sysMusicAlbum.setTitle(musicExcel.getAlbumTitle());
            sysMusicAlbum.setFileType(sysTvData.getId());
            List<SysMusicAlbum> sysMusicAlbumList = sysMusicAlbumService.selectSysMusicAlbumList(sysMusicAlbum);
            Boolean musicAlbumsFlag = false;
            if (sysMusicAlbumList.size() > 0) {
                sysMusicAlbum = sysMusicAlbumList.get(0);
                musicAlbumsFlag = true;
            }
            String logoAddress = musicExcel.getLogoAddress();
            File logoFile = new File(fileRootPath+File.separator +logoAddress);
            if (logoFile.exists()) {
                sysMusicAlbum.setImgAddress(logoFile.getAbsolutePath());
                if (sourceType.equals(SourceTypeEnum.init.getCode())){
                    String substring = sysMusicAlbum.getImgAddress().substring(PlayControlConfig.getNginxRootPath().length());
                    sysMusicAlbum.setImgUrl(PlayControlConfig.getMappingPath()+substring);
                }else{
                    String substring = sysMusicAlbum.getImgAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
                    sysMusicAlbum.setImgUrl(PlayControlConfig.getDiskMappingPath()+substring);
                }
            }else{
                continue;
            }
            String albumTitlekeyWord = ChinesePinyinConverter.convertToPinyin(musicExcel.getAlbumTitle()).replaceAll("\\s+", "");
            sysMusicAlbum.setKeyWord(albumTitlekeyWord);
            if (musicAlbumsFlag) {
                sysMusicAlbumService.updateSysMusicAlbum(sysMusicAlbum);
            }else{
                logger.info("新增专辑:"+musicExcel.getAlbumTitle());
                sysMusicAlbumService.insertSysMusicAlbum(sysMusicAlbum);
            }

            //开始进行歌曲录入
            SysMusicData sysMusicData = new SysMusicData();
            sysMusicData.setTitle(musicExcel.getTitle());
            sysMusicData.setSinger(musicExcel.getSinger());
            sysMusicData.setAlbumId(sysMusicAlbum.getId());
            sysMusicData.setTypeData(sysTvData.getId());
            List<SysMusicData> sysMusicDataList = sysMusicDataService.selectSysMusicDataList(sysMusicData);
            Boolean musicDataFlag = false;
            if (sysMusicDataList.size() > 0) {
                sysMusicData = sysMusicDataList.get(0);
                musicDataFlag = true;
            }
            String songAddress = musicExcel.getVideoAddress();
            File songFile = new File(fileRootPath+File.separator +songAddress);
            if (songFile.exists()) {
                sysMusicData.setVideoAddress(songFile.getAbsolutePath());
                if (sourceType.equals(SourceTypeEnum.init.getCode())){
                   String substring = sysMusicData.getVideoAddress().substring(PlayControlConfig.getNginxRootPath().length());
                    sysMusicData.setMusicUrl(PlayControlConfig.getMappingPath()+substring);
                }else{
                    String substring = sysMusicData.getVideoAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
                    sysMusicData.setMusicUrl(PlayControlConfig.getDiskMappingPath()+substring);
                }

            }else {
                continue;
            }

            String lyricAddress = musicExcel.getLyricAddress();
            File lyricFile = new File(fileRootPath+File.separator +lyricAddress);
            if (lyricFile.exists()) {
                sysMusicData.setLyricAddress(lyricFile.getAbsolutePath());
                if (sourceType.equals(SourceTypeEnum.init.getCode())){
                    String substring = sysMusicData.getLyricAddress().substring(PlayControlConfig.getNginxRootPath().length());
                    sysMusicData.setLyricUrl(PlayControlConfig.getMappingPath()+substring);
                }else {
                    String substring = sysMusicData.getLyricAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
                    sysMusicData.setLyricUrl(PlayControlConfig.getDiskMappingPath()+substring);
                }


            }else {
                continue;
            }
            sysMusicData.setAlbumTitle(musicExcel.getAlbumTitle());
            sysMusicData.setAlbumId(sysMusicAlbum.getId());
            sysMusicData.setTypeData(sysTvData.getId());
            String titlekeyWord = ChinesePinyinConverter.convertToPinyin(musicExcel.getTitle()).replaceAll("\\s+", "");
            sysMusicData.setKeyWord(titlekeyWord);
            sysMusicData.setContentTitle(musicExcel.getAlbumTitle());
            sysMusicData.setImgAddress(sysMusicAlbum.getImgAddress());
            sysMusicData.setTaskId(taskId);
            sysMusicData.setImgUrl(sysMusicAlbum.getImgUrl());
            sysMusicData.setSouceType(sourceType);
            SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
            String s = MusicWordTool.serverReadFile(sysBoard.getUrl()+sysMusicData.getLyricUrl());
            sysMusicData.setSongWord(s);
            if (musicDataFlag) {
                logger.info("更新歌曲:"+sysMusicData.getTitle()+"========="+sysMusicData.getVideoAddress()+"^^^^^^^^^^"+taskId);
                sysMusicDataService.updateSysMusicData(sysMusicData);
            }else {
                logger.info("新增歌曲:"+sysMusicData.getTitle()+"^^^^^^^^^^"+taskId);
                sysMusicDataService.insertSysMusicData(sysMusicData);
            }
        }
    }

    public void copyUsbInsertByDirPath(String usbDirPath,MusicExcel musicExcel,String taskId){
        logger.info("检测到服务器插入的硬盘中待导入的歌曲信息:"+musicExcel.toString());

        SysTvData sysTvData = new SysTvData();
        sysTvData.setName(musicExcel.getFileType());
        sysTvData.setTypeId(Long.valueOf(SysTvType.songDefaultId));
        List<SysTvData> sysTvDataList = sysTvDataService.selectSysTvDataList(sysTvData);
        if (StringUtils.isNotNull(sysTvDataList)){
            sysTvData = sysTvDataList.get(0);
        }else{
            sysTvDataService.insertSysTvData(sysTvData);
        }

        //录入专辑
        SysMusicAlbum sysMusicAlbum = new SysMusicAlbum();
        sysMusicAlbum.setTitle(musicExcel.getAlbumTitle());
        sysMusicAlbum.setFileType(sysTvData.getId());
        List<SysMusicAlbum> sysMusicAlbumList = sysMusicAlbumService.selectSysMusicAlbumList(sysMusicAlbum);
        Boolean musicAlbumsFlag = false;
        if (sysMusicAlbumList.size() > 0) {

            sysMusicAlbum = sysMusicAlbumList.get(0);
            musicAlbumsFlag = true;
        }
        String logoAddress = musicExcel.getLogoAddress();
        File logoFile = new File(usbDirPath +logoAddress);
        if (logoFile.exists()) {

            String usbAddress = PlayControlConfig.getHardDiskFilePath()+File.separator+"usbData"+File.separator+logoAddress;
            FileMoveUsingIO.moveFile(logoFile.getAbsolutePath(),usbAddress);

            sysMusicAlbum.setImgAddress(usbAddress);
            String substring = sysMusicAlbum.getImgAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
            sysMusicAlbum.setImgUrl(PlayControlConfig.getDiskMappingPath()+substring);

        }else{
            return;
        }
        String albumTitlekeyWord = ChinesePinyinConverter.convertToPinyin(musicExcel.getAlbumTitle()).replaceAll("\\s+", "");
        sysMusicAlbum.setKeyWord(albumTitlekeyWord);
        if (musicAlbumsFlag) {
            sysMusicAlbumService.updateSysMusicAlbum(sysMusicAlbum);
        }else{
            sysMusicAlbumService.insertSysMusicAlbum(sysMusicAlbum);
        }


        //开始进行歌曲录入
        SysMusicData sysMusicData = new SysMusicData();
        sysMusicData.setTitle(musicExcel.getTitle());
        sysMusicData.setSinger(musicExcel.getSinger());
        sysMusicData.setAlbumId(sysMusicAlbum.getId());
        sysMusicData.setTypeData(sysTvData.getId());
        List<SysMusicData> sysMusicDataList = sysMusicDataService.selectSysMusicDataList(sysMusicData);
        Boolean musicDataFlag = false;
        if (sysMusicDataList.size() > 0) {
            sysMusicData = sysMusicDataList.get(0);
            musicDataFlag = true;
        }
        String songAddress = musicExcel.getVideoAddress();
        File songFile = new File(usbDirPath +songAddress);
        if (songFile.exists()) {
            String usbAddress = PlayControlConfig.getHardDiskFilePath()+File.separator+"usbData"+File.separator+songAddress;
            FileMoveUsingIO.moveFile(songFile.getAbsolutePath(),usbAddress);

            sysMusicData.setVideoAddress(usbAddress);
            String substring = sysMusicData.getVideoAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
            sysMusicData.setMusicUrl(PlayControlConfig.getDiskMappingPath()+substring);
        }else {
            return;
        }

        String lyricAddress = musicExcel.getLyricAddress();
        File lyricFile = new File(usbDirPath +lyricAddress);
        if (lyricFile.exists()) {

            String usbAddress = PlayControlConfig.getHardDiskFilePath()+File.separator+"usbData"+File.separator+lyricAddress;
            FileMoveUsingIO.moveFile(lyricFile.getAbsolutePath(),usbAddress);
            sysMusicData.setLyricAddress(usbAddress);

            String substring = sysMusicData.getLyricAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
            sysMusicData.setLyricUrl(PlayControlConfig.getDiskMappingPath()+substring);
        }else {
            return;
        }
        sysMusicData.setAlbumTitle(musicExcel.getAlbumTitle());
        sysMusicData.setAlbumId(sysMusicAlbum.getId());
        sysMusicData.setTypeData(sysTvData.getId());
        String titlekeyWord = ChinesePinyinConverter.convertToPinyin(musicExcel.getTitle()).replaceAll("\\s+", "");
        sysMusicData.setKeyWord(titlekeyWord);
        sysMusicData.setContentTitle(musicExcel.getAlbumTitle());
        sysMusicData.setImgAddress(sysMusicAlbum.getImgAddress());
        sysMusicData.setImgUrl(sysMusicAlbum.getImgUrl());
        sysMusicData.setTaskId(taskId);
        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String s = MusicWordTool.serverReadFile(sysBoard.getUrl()+sysMusicData.getLyricUrl());
        sysMusicData.setSongWord(s);
        sysMusicData.setSouceType(SourceTypeEnum.usb.getCode());
        if (musicDataFlag) {
            logger.info("USB导入更新歌曲:"+sysMusicData.getTitle()+"======="+sysMusicData.getVideoAddress()+"^^^^^^^^^^"+taskId);
            sysMusicDataService.updateSysMusicData(sysMusicData);
        }else {
            logger.info("USB导入新增歌曲:"+sysMusicData.getTitle()+"^^^^^^^^^^"+taskId);
            sysMusicDataService.insertSysMusicData(sysMusicData);
        }
    }



}
